package com.smart.car.record.service.impl;

import cn.hutool.core.exceptions.NotInitedException;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.smart.car.common.res.bean.ResponseResult;
import com.smart.car.common.res.constant.Constants;
import com.smart.car.record.entity.RecordChargeRule;
import com.smart.car.record.entity.RecordChargeRuleExample;
import com.smart.car.record.mapper.RecordChargeRuleMapper;
import com.smart.car.record.service.RecordChargeRuleService;
import com.smart.car.record.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 收费规则业务实现
 */
@Slf4j
@Service
public class RecordChargeRuleServiceImpl implements RecordChargeRuleService {

	@Autowired
	private RecordChargeRuleMapper ruleMapper;
	
	@Autowired
	private RedisService redisService;

	/**
	* 因为是默认的初始化数据，要求必须存在
	* @author: zoutao
	* @date: 2021/4/6
	*/
	@Override
	public List<RecordChargeRule> list() {
		RecordChargeRuleExample example = new RecordChargeRuleExample();
		example.setOrderByClause("start asc");
		List<RecordChargeRule> rules = ruleMapper.selectByExample(example);
		log.info("获取到计费规则，如下："+rules.size());
		if (rules.isEmpty() && rules.size()<=0) {
			throw new NotInitedException("数据库没有初始化的收费规则，请至少增加一条！");
		}
		return rules;
	}

	@Override
	public int refresh() {
		RecordChargeRuleExample example = new RecordChargeRuleExample();
		example.setOrderByClause("start asc");
		List<RecordChargeRule> list = ruleMapper.selectByExample(example);
		if (ObjectUtil.isNotEmpty(list)) {
			redisService.cacheObject(Constants.cache.CHARGINGRULE, JSONObject.toJSONString(list));
			log.info("刷新计费规则到Redis缓存!");
		}
		return Constants.SUCCESS;
	}



	@Override
	public Object insert(RecordChargeRule recordChargeRule) {
		// valid
		if (recordChargeRule == null) {
			return ResponseResult.fail("必要参数缺失");
		}
		ruleMapper.insert(recordChargeRule);
		return ResponseResult.ok();
	}

	@Override
	public ResponseResult<String> delete(String id) {
		int ret = ruleMapper.deleteByPrimaryKey(id);
		return ret>0?ResponseResult.ok():ResponseResult.fail();
	}


	@Override
	public ResponseResult<String> update(RecordChargeRule recordChargeRule) {
		//int ret = ruleMapper.updateByExample(recordChargeRule);
		int ret =0;
		return ret>0?ResponseResult.ok():ResponseResult.fail();
	}

	@Override
	public RecordChargeRule load(String id) {
		return ruleMapper.selectByPrimaryKey(id);
	}


//	@Override
//	public Map<String,Object> pageList(int offset, int pagesize) {
//		List<RecordChargeRule> pageList = ruleMapper.pageList(offset, pagesize);
//		int totalCount = ruleMapper.pageListCount(offset, pagesize);
//		// result
//		Map<String, Object> result = new HashMap<String, Object>(8);
//		result.put("pageList", pageList);
//		result.put("totalCount", totalCount);
//		return result;
//	}


}
